import {message} from 'antd';
import {queryCheck} from '../services/api';
import {backUser, checkUser} from '../services/user';


export default {
    namespace: 'check',

    state: {
        data: {info: {}, service: {}, user: {}},
        loading: true,
    },

    effects: {
        * fetchCheck({payload, callback}, {call, put}) {
            yield put({
                type: 'changeLoading',
                payload: {loading: true},
            });
            const response = yield call(queryCheck, payload);

            yield put({
                type: 'show',
                payload: {data: response.data},
            });
            yield put({
                type: 'changeLoading',
                payload: {loading: false},
            });
            if (callback) {
                callback();
            }
        },
        * back({payload, callback}, {call, put}) {
            yield put({
                type: 'changeLoading',
                payload: true,
            });
            const response = yield call(backUser, payload);
            if (response.status) {
                message.success(response.message);
            } else {
                message.error(response.message);
            }
            if (response && callback) {
                callback(response);
            }
            yield put({
                type: 'changeLoading',
                payload: false,
            });
        },
        * accredit({payload, callback}, {call, put}) {
            yield put({
                type: 'changeLoading',
                payload: true,
            });
            const response = yield call(checkUser, payload);
            if (response.status) {
                message.success('审核成功');
            } else {
                message.error('审核失败');
            }
            if (response && callback) {
                callback(response);
            }
            yield put({
                type: 'changeLoading',
                payload: false,
            });
        },
        * fetchAdvanced(_, {call, put}) {
            yield put({
                type: 'changeLoading',
                payload: {advancedLoading: true},
            });
            const response = yield call(queryAdvancedProfile);
            yield put({
                type: 'show',
                payload: response,
            });
            yield put({
                type: 'changeLoading',
                payload: {advancedLoading: false},
            });
        },
    },

    reducers: {
        show(state, {payload}) {
            return {
                ...state,
                ...payload,
            };
        },
        changeLoading(state, {payload}) {
            return {
                ...state,
                ...payload,
            };
        },
    },
};
